const parseUrl = require('parseurl');
const ReportData = require('./models/ReportData.js');
function Statistic(eventName) {
    if (eventName  == 'page') {
        return function(req, res, next) {
            let pathname  = parseUrl(req).pathname;
            reportData('page', pathname, 1);
            next();
        };
    }
}

function reportData(eventName, key, value, tag) {

    if(tag == 'undefined' || typeof tag == 'undefined') {
        tag = 'NULL';
    }

    const data = new ReportData({
        event_name  : eventName,
        event_key   : key,
        event_value : value,
        report_time : Date.now(),
        tag         : tag,
    });

    data.save(function(err, doc){
        if(err) {
            console.log('report data is error , key: ' + key + ', errDesc : ' + err);
        }
    });
}

module.exports = Statistic;
module.exports.reportData = reportData;
